package cn.mandifal.cart.controller;
import java.util.List;

import cn.mandifal.pojo.DpAddress;
import cn.mandifal.user.service.AddressService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;

import entity.PageResult;
import entity.Result;
/**
 * 收货地址 controller
 * @author shun
 *
 */
@RestController
@RequestMapping("/addressController")
public class AddressController {

	@Reference
	private AddressService addressService;

	/**
	 * 增加
	 * @param address
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody DpAddress address){
		try {
			addressService.add(address);
			return  Result.success("增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param address
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody DpAddress address){
		try {
			addressService.update(address);
			return Result.success("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed( "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public DpAddress findOne(Long id){
		return addressService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long ids){
		try {
			addressService.delete(ids);
			return Result.success("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("删除失败");
		}
	}
	

	@RequestMapping("/findListByLoginUser")
	public List<DpAddress> findListByLoginUser(){
		//获取登陆用户
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		return addressService.findListByUserId(username);		
	}
	
}
